package rili;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2020/9/14 15:50
 */
public class Rili {
    private int year;
    private int month;

    public Rili(int year, int month) {
        this.year = year;
        this.month = month;
    }

    //抽象出来的日历模板
    public void run(){
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        int day = dayOfMonth(year, month);
        //打多少个空格
        for (int i = 0; i < day; i++) {
            System.out.print("\t");
        }

        //日历模板
        for (int i = 1; i <= monthNum(year, month); i++) {
            System.out.print(i+"\t");
            if (i==(7-day)) {
                System.out.println();
            }else if (i%7==(7-day)&&day!=0){
                System.out.println();
            }else if (day==0&&i%7==0){ //修复一个bug
                System.out.println();
            }

        }
    }
    //1990年一月一日是星期一


    //这个月的第一天是星期几？
    public int dayOfMonth(int year,int month) {
        if (year==1990){
            return (1+dayOfYear(year,month))%7;
        }

      return ((year-1990)*365+leapYearNum(year)+dayOfYear(year,month))%7;
    }


    //是闰年吗
    public boolean isleapYear(int year) {
        if (year%400==0) return true;
        if (year % 4 == 0 && year % 100 != 0) {
            return true;
        }
        return false;
    }


    //这个月有多少天
    public int monthNum(int year,int month){
        if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
            return 31;
        }else if (month==4||month==6||month==9||month==11){
            return 30;
        }else if (month==2&&isleapYear(year)){ //如果是闰年的话2月就是29天
            return 29;
        }else return 28;
    }

    //从1990年到现在有多少个闰年？
    public int leapYearNum(int year){
        int count = 0;
        for (int i = 1990; i <= year; i++) {
            if(isleapYear(i)) count++;
        }
        return count;
    }


    //从今年到现在过了多少天了
    public int dayOfYear(int year,int month){
        if (month==1) return 0;
        int sum = 0;
        for (int i = 1; i < month; i++) {
            sum+=monthNum(year,i);
        }

        return sum;
    }
}
